﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HA.ModFlow.Domain.FileLayer;

namespace HA.ModFlow.Domain.DomainLayer
{
    [Serializable]
   public  class GHBCollection:BoundaryCollecionBase
    {

        public GHBCollection(int nrows, int ncolumns, int nlayers):base(nrows,ncolumns,nlayers)
        {
            this._innerList = new List<GeneralHead>();
 
        }


        List<GeneralHead> _innerList;

        public List<GeneralHead> InnerList
        {
            get { return _innerList; }
           
        }




        #region 公共方法

        public List<GeneralHead> GetGHB(int index)
        {
            List<GeneralHead> heads = new List<GeneralHead>();


            foreach (GeneralHead item in this._innerList)
            {
                if (item.Index==index)
                {

                    heads.Add(item);
                    
                }

                
            }

            return heads;
        }


        public void UpdateGHB(List<GeneralHead> ghbs, List<int> indices)
        {

            //取得所有的index元素

            List<GeneralHead> orgHeads = new List<GeneralHead>();

            foreach (int index in indices)
            {
                orgHeads.AddRange(this.GetGHB(index));
                
            }


            foreach (GeneralHead head in orgHeads)
            {
                this._innerList.Remove(head);
                

            }


            this._innerList.AddRange(ghbs);

            this.AskCollectionChanged(new SCColleconChangedEventArgs(this._innerList,SCCollectionChangedType.RESET,SCType.GHB));



 
        }


        public void ResetGHB(List<GeneralHead> ghbs)
        {
            this._innerList.Clear();

            this._innerList.AddRange(ghbs);

            this.AskCollectionChanged(new SCColleconChangedEventArgs(this._innerList, SCCollectionChangedType.RESET, SCType.GHB));


        }

        public PGeneralHeadBoundary ToGHBPack()
        {
            PGeneralHeadBoundary p = new PGeneralHeadBoundary();

            p.MXACTB = this._innerList.Count;
            //p.IGHBCB = 0;

            for (int i = 0; i < this.NPeriods;i++ )
            {

                SPGeneralHead sp = new SPGeneralHead();



                if (i == 0)
                {

                    sp.ITMP = this._innerList.Count;

                    foreach (GeneralHead item in this._innerList)
                    {
                        GPointValue pv = new GPointValue(item.K, item.I, item.J, item.Head[i], item.Conductance[i]);


                        sp.GeneralData.Add(pv);

                    }


                }

                else
                {

                    if (this.IsUsePrecious[i] == true)
                    {
                        sp.ITMP = -1;

                    }
                    else
                    {

                        sp.ITMP = this._innerList.Count;

                        foreach (GeneralHead item in this._innerList)
                        {
                            GPointValue pv = new GPointValue(item.K, item.I, item.J, item.Head[i], item.Conductance[i]);


                            sp.GeneralData.Add(pv);

                        }

                    }
                }


                p.GeneralHeads.Add(sp);



            }
            




            return p;
            
        }
        public override void _stressPeriods_StressPeriodChanged(object sender, StressPeriodChangedEventArgs e)
        {

            //基类的函数必须调用

            base._stressPeriods_StressPeriodChanged(sender, e);
            

            List<StressPeriod> lst = (List<StressPeriod>)e.Param;
            foreach (GeneralHead   item in this._innerList)
            {
                item.OnResetSperiods(lst.Count);
            }
        }


        public void Clear()
        {
            this._innerList.Clear();

            this.AskCollectionChanged(new SCColleconChangedEventArgs(this._innerList, SCCollectionChangedType.RESET, SCType.GHB));

        }


        public override void OnGridReset(int nrows, int ncols, int nlayers)
        {
            base.OnGridReset(nrows, ncols, nlayers);

            this.Clear();


        }

        #endregion

        List<GeneralHead> _tempGeneralHeads = new List<GeneralHead>();
        bool _haveTempData = false;
        public bool HaveTempData
        {
            get { return _haveTempData; }
        }

        public void AddTempGeneralHeads(List<GeneralHead> generalHeads)
        {
            this._tempGeneralHeads.AddRange(generalHeads);
            this._haveTempData = true;
        }


        /// <summary>
        /// 将暂存的数据提供给核心，并清空对应的选项
        /// </summary>
        public void CommitTempGeneralHeads()
        {
            if (this._haveTempData)
            {
                this.ResetGHB(this._tempGeneralHeads);
                this._tempGeneralHeads.Clear();
            }
            this._haveTempData = false;
        }

    }
}
